<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <!-- header -->
    <%@ include file="/WEB-INF/views/include/header.jsp" %>
</head>
<body class="no-skin">
<%--上传成功模态框--%>
<div class="shade" style="display: none;"></div>
<div class="warning" style="display: none;">
    <p class="title"><em></em><span class="close">X</span></p>
    <p class="cont"></p>
    <div class="la-square-jelly-box la-3x">
        <div></div>
        <div></div>
    </div>
</div>
<!-- /section:basics/navbar.layout -->
<div class="main-container" id="main-container">

    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <!-- /section:settings.box -->
                <div class="row">
                    <div class="col-xs-12">
                        <div class="pull-left">
                            <button class="btn btn-sm btn-info" id="query">条件搜素</button>
                            <button class="btn btn-sm btn-success" id="create">撤销回调</button>
                            <button class="btn btn-sm btn-danger" id="backResult">消息回复</button>
                            <button class="btn btn-sm btn-default" id="detail">详情</button>
                            <button class="btn btn-sm btn-warning" id="forward">转发</button>
                        </div>
                        <div class="pull-right">
                            <button class="btn btn-sm btn-info" id="all">全部消息</button>
                            <button class="btn btn-sm btn-success" id="noRead">未读消息</button>
                            <button class="btn btn-sm btn-default" id="read">已读消息</button>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-xs-12">
                        <!-- PAGE CONTENT BEGINS -->
                        <!-- jqGrid表格 -->
                        <table id="gridTable"></table>
                        <!-- jqGrid工具类 -->
                        <div id="gridPager"></div>
                        <!-- 查询对话框 -->
                        <div style="display: none;">
                            <div class="row" id="queryDialog"></div>
                        </div>
                        <!-- PAGE CONTENT ENDS -->
                    </div><!-- /.col -->
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div>
    </div><!-- /.main-content -->

    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
    </a>

    <%@ include file="/WEB-INF/views/include/dialog.jsp" %>
</div><!-- /.main-container -->
</div>

<!-- footer -->
<%@ include file="/WEB-INF/views/include/footer.jsp" %>

<script type="text/javascript">
    // 基本路径
    var baseUrl = "<s:url value="/message/"></s:url>"

    // jqgrid的表格高度
    var height = 320;
    // jqgrid表格行头的中文
    // var colNames = ['', 'ID', "任务名", "派发id", "反馈单位", "消息类型", "消息内容","消息读取状态"];
    var colNames = ['', '消息编号', "任务名", "反馈单位", "消息内容", "消息类型","消息读取状态"];

    var colModel = [
        {name: 'taskId', width: 60, hidden: true},
        {name: 'sysId',index: 'taskId', width: 250, editable: true, key: true },
        {name: 'taskName', index: 'taskName', width: 250, editable: true},
        // {name: 'sendId', index: 'sendId', width: 250, editable: true},
        {name: 'sendDeptName', index: 'sendDeptName', width: 250, editable: true},
        {name: 'content', index: 'content', width: 250, editable: true},
        {name: 'type', index: 'type', width: 250, editable: true, formatter: categoryIdFormatter},
        {name: 'state', index: 'state', width: 250, editable: true, formatter: fileFormatter}
    ];

    /**
     * 任务类型格式化
     */
    function categoryIdFormatter(data) {
        return data == 1 ?  "反馈" : "撤销申请";
    }

    /**
     * 任务状态格式化
     */
    function fileFormatter(data) {
       return data == 1 ? "已读" : "未读"
    }


    //下载按钮
    function downDetail(id) {
        var url = "/filesUpload/download/" + id;
        window.open(url);
    }

    // jqgrid的具体执行方法
    jqgrid({
        gridTableId: "#gridTable",
        gridPagerId: "#gridPager",
        autoScroll: false,
        shrinkToFit: false,
        onSelectRow: function (rowid, status) {
            var selectedRowValue = $("#gridTable").jqGrid('getRowData', rowid);
            var types = selectedRowValue.type;//获取任务状态
            //只有已经反馈和待反馈才可以打开
            if (types == "撤销申请") {
                $("#backResult").attr("disabled", true);
                $("#create").attr("disabled", false);
            } else {

                $("#backResult").attr("disabled", false);
                $("#create").attr("disabled", true);
            }
        }
    });

    //重置表单
    function reset() {
        resetForm("#formDialog form");
    }

    //加载查询对话框
    $("#queryDialog").load(getUrl("filter"));

    //显示查询对话框
    var queryDialog = $("#queryDialog");
    $("#query").click(function () {
        showDialog({
            dialogTitle: '条件搜素',
            dialogBody: queryDialog,
            dialogFoot: "<button type='button' class='btn btn-primary' onclick='query()'>查询</button>" +
            "<button type='button' class='btn btn-pink' onclick='reset()'>重置</button>" +
            "<button type='button' class='btn btn-default' data-dismiss='modal'>关闭</button>"
        });
    });

    //具体查询方法
    function query() {
        var exportData = $('#formDialog form').serialize();
        seachJqgird("#gridTable", "#formDialog form");
        $("#formDialog").modal('hide');
    }

    // 重置创建表单
    function resetCreate() {
        resetForm("#formDialog form");
        // 清除关于会议纪要的内容,及隐藏按钮
        $('#departContent').empty();
        $("#addDepart").css("display", "none");
    }

    // 显示创建对话框
    $("#create").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "撤销协调",
            dialogUrl: getUrl("dispose"),
            dialogFoot: '<button type="button" class="btn btn-primary" onclick="create()">添加</button>' +
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });

    // 保存数据
    function create() {

        // 先验证表单
        if(!$("#formDialog form").valid()) {
            return;
        }

        var hasDepart = $('#departContent').html();
        if (hasDepart == null || hasDepart == '') {
            alertMessage("请添加承办单位!");
            return;
        }

        // 判断承办单位是否重复
        var departIdList = new Array();
        $("select[name$='.userId']").each(function () {
            departIdList.push($(this).val());
        });
        var sortList = departIdList.sort();
        for (var i = 0; i < sortList.length - 1; i++) {
            if (sortList[i] == sortList[i + 1]) {
                alertMessage("承办单位重复,请修改!");
                return;
            }
        }

        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("dispose"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
    }
    //添加数据
    function backResult(){
        // 先验证表单
        if(!$("#formDialog form").valid()) {
            return;
        }
        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("backResult"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
        // seachJqgird("#gridTable", "#formDialog form");
        $("#formDialog").modal('hide');
    }
    //详情
    $("#backResult").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "消息回复",
            dialogUrl: getUrl("backResult"),
            dialogFoot:  "<button type='button' class='btn btn-pink' onclick='backResult()'>确认</button>" +
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });


    //全部消息
    $("#all").click(function () {
        var url = '/message';
        $("#gridTable").jqGrid('setGridParam', {
            url: url,
            datatype: 'json',
            postData: {'state': '2'}, //发送数据
        }).trigger("reloadGrid"); //重新载入
    });

    //已读消息
    $("#read").click(function () {
        var url = '/message';
        $("#gridTable").jqGrid('setGridParam', {
            url: url,
            datatype: 'json',
            postData: {'state': '1'}, //发送数据
        }).trigger("reloadGrid"); //重新载入
    });
    //未读消息
    $("#noRead").click(function () {
        var url = '/message';
        $("#gridTable").jqGrid('setGridParam', {
            url: url,
            datatype: 'json',
            postData: {'state': '0'}, //发送数据
        }).trigger("reloadGrid"); //重新载入
    });

    //详情
    $("#detail").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "消息详情",
            dialogUrl: getUrl("detail"),
            dialogFoot: '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });
    /**
     * 弹出消息对话框
     * @param msg
     */
    function alertMessage(msg) {
        msg = msg == null ? '' : msg;
        bootbox.dialog({
            title: "消息对话框",
            message: msg,
            buttons: {
                "danger": {
                    "label": '确定',
                    "className": "btn-sm btn-danger"
                }
            }
        });
    }
    //转发 forward
    $("#forward").click(function () {
        showEditDialog({
            tableId: "#gridTable",
            dialogTitle: "消息详情",
            dialogUrl: getUrl("forwardTo"),
            dialogFoot: '<button type="button" class="btn btn-success" onclick="forwardTo()">转发</button>' +
            '<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>',
            errorTitle: "消息对话框",
            errorMutiSelectMessage: '您当前选中多行，请重新选择',
            errorUnSelectMessage: '您当前尚未选中一行，请选择后再试',
            errorOk: '确定'
        });
    });
    function forwardTo() {
        executeSaveOrUpdate({
            formId: "#formDialog form",
            dialogId: "#formDialog",
            url: getUrl("forwardTo"),
            tableId: "#gridTable",
            successMessage: "保存成功",
            successTitle: '保存对话框',
            failMessage: '保存失败',
            failTitle: '消息对话框'
        });
    }
</script>

</body>
</html>